import axios from 'axios';
import VueAxios from 'vue-axios'
import Vue from 'vue'
import router from '../router'
import { Message } from 'element-ui';
import LoadingUtil from './loadingUtil'


const isProduction = process.env.NODE_ENV === 'production'

const loadingUtil = new LoadingUtil('.content-area')

axios.interceptors.request.use(
  (config) => {
    // config.headers.Authorization = "Bearer " + localStorage.token
    if (!config.hideLoading) {
      loadingUtil.showLoading()
    }
    return config
  },
  (error) => {
   loadingUtil.closeLoadingSlow()
    return Promise.reject(error);
});
axios.interceptors.response.use(
  (response) => {
   loadingUtil.closeLoadingSlow()
    return response;
  },
  (error) => {
   loadingUtil.closeLoadingSlow()
    if (error.response.status == 403) {
      Message({
        message: error.response.data.message,
        type: 'error'
      });
      router.push({
        name: 'Login'
      })
    } else {
      return Promise.reject(error.response.data);
    }
});

axios.defaults.baseURL = isProduction ? `//api.sunxq.top` : '//abc.sunxq.top:8000'
axios.defaults.withCredentials = true

Vue.use(VueAxios, axios)